package gui;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import users.Manager;

public class ProcessShipment extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5800521769002126083L;
	private Manager manager;
	private JTextField sidInput;
	private JButton checkShipment;
	private JPanel shipPanel;
	private ResultSet rs;
	private ResultSet rsList;
	private JPanel rDPanel;
	private JTable rTable;
	private JTable rNewTable;
	private JScrollPane scrollPane2;
	private JButton checkPrice;
	//private JFrame main;
	
	public ProcessShipment(Manager manager) throws SQLException{
		this.manager = manager;
		//main = new JFrame();
		add(inputPanel());
		add(showTable());
		//main.pack();
		//main.setVisible(true);
		//add(main);
		if(checkShipment.isSelected()){
			add(updateTable(rs));
			validate();
		}
		else{
			return;
		}
	}
	
	private Component inputPanel(){
		JPanel inputP = new JPanel();
		inputP.setLayout(new GridLayout(1, 2));
		
		inputP.add(new JLabel("Please Enter Sid of Shipment: "));
        sidInput = new JTextField();
        sidInput.setPreferredSize(new Dimension(10, 20));
        inputP.add(sidInput);
        
        checkShipment = new JButton("Check Shipment");
		checkShipment.addActionListener(new checkShipments());
		inputP.add(checkShipment);
        
        return add(inputP);
	}

	private Component showTable() throws SQLException{
		
		rDPanel = new JPanel(new FlowLayout());
		TitledBorder title = BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), "Detail List");
		rDPanel.setBorder(title);
		
		String[] columnNames = {"sid", 
								"supName", 
								"storeName",
								"shipDate",};
		rsList = manager.listShipments();
		rTable = rsMakeTable(rsList, columnNames);
		
		JScrollPane scrollPane = new JScrollPane(rTable);
		scrollPane.setSize(300, 300);
        return rDPanel.add(scrollPane);
	}
	
	private static JTable rsMakeTable (ResultSet rs, String[] columnNames) throws SQLException{
		List<Object[]> rows = new ArrayList<Object[]>();
		
		while (rs.next()){
			Object[] row = new Object[columnNames.length+1];
			
			for (int i=0; i<columnNames.length; i++){
				Object value = rs.getObject(columnNames[i]);
				row[i] = value;
			}			
			rows.add(row);
		}
		
		Object[][] data = new Object[rows.size()][columnNames.length+1];
		
		for (int i=0; i<rows.size(); i++){
			data[i] = rows.get(i);
		}
		
		JTable shipTable = new JTable(data, columnNames);
		shipTable.setPreferredScrollableViewportSize(new Dimension(500, 70));
        shipTable.setFillsViewportHeight(true);
		
		return shipTable;
	}
	
	public class checkShipments implements ActionListener{

	private ResultSet rs2;

	public void actionPerformed(ActionEvent arg0) {
		try {
			
			rs = manager.listItems(Integer.valueOf(sidInput.getText()));
			rs2 = manager.listItems(Integer.valueOf(sidInput.getText()));
			//manager.updatePrice(rs);
			updatePrice(rs);
			JOptionPane.showMessageDialog(checkShipment, "remember to check updated sell prices");
			add(updateTable(rs2));
			setVisible(true);
			validate();
			
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(checkShipment,
				    "The shipment does not exist.\nPlease retry.", "Shipment Error",
				    JOptionPane.ERROR_MESSAGE);
			System.out.println("A problem occurred while checking shipment");
			}
		}
	}
	
	private void updatePrice(ResultSet rs) throws SQLException {
		
		int i=0;
		String[] item = new String[150];
		int[] itemPrice = new int[150];
		while(rs.next()){
			item[i] = rs.getString("upc");
			itemPrice[i] = (int) (rs.getInt("supPrice") * 1.20);
			i++;
		}
		//System.out.print(item);
		manager.updatePrice(item, itemPrice);
	}
	
	private Component updateTable(ResultSet rs) throws SQLException{
		
		shipPanel = new JPanel(new FlowLayout());
		TitledBorder title = BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), "Detail List");
		shipPanel.setBorder(title);
		
		
		String[] newNames = {"upc",
							"supPrice", 
							"quantity",};
		rNewTable = rsMakeTable(rs, newNames);
		
		scrollPane2 = new JScrollPane(rNewTable);
		shipPanel.add(scrollPane2);
		return add(shipPanel);
	}
}